  
var pro_load_page = 2,pro_load_flag = true;

  var mainI={
   	navi: $('.index-nav'),//顶部导航box
   	navbar1:$('.index-nav .mui-col-xs-3:nth-child(1)'),//顶部导航列
   	navbar2:$('.index-nav .mui-col-xs-3:nth-child(2)'),
   	navbar3:$('.index-nav .mui-col-xs-3:nth-child(3)'),
   	navbar4:$('.index-nav .mui-col-xs-3:nth-child(4)'),
   	init: function(){
   		var upArrow=$('.down'),//上箭头
   	  upArrowImg=$('.down img'),
   		upArrowText= $('.down .text');
   		upArrow.hide();
   		localStorage.setItem('sensH',0);
   		this.navi.find('i').hide();
   		$('footer .mui-col-xs-3:first p').removeClass('font-active');
   		$('footer .mui-col-xs-3:first p').addClass('font-active');
   		$('footer .mui-col-xs-3:first img').attr('src','../img/icon/index2.png');
		if (page.isStore()){
			app.tpl($('.step1'),'tpl-step1',page.getData());
			echo.init();
		}else {
   		app.ajax(
				'/Index/Index/index',
				null,
				function(res){
					if(res.code == 200){
						page.savaData(res);
						app.tpl($('.step1'),'tpl-step1',res);
						echo.init();
					}else{
						app.prompt('洋玩易提示','系统繁忙，请稍候再试');
					}
				}
			);
		}
   	},
   	navJump:function(t){
			$(t).find('h2').addClass('font-active');
			$(t).siblings().find('h2').removeClass('font-active');
			var index=$(t).index('.index-nav .mui-col-xs-3');
			var title=$('[data-nav]');
			var height = 0;
   		if($('header').html()){
   			height = parseInt($('header').height());
   		}

			for(var i=0;i<title.length;i++){
				 if($(title[i]).attr('data-nav') == index){
		            scrollH=$(title[i]).offset().top-45-height;
		            $('html,body').animate({scrollTop:scrollH}, 800);
		            return false;
		        }
			}
			return false;
   	},
   	navScroll: function(){//导航控制滚动
   		var upArrow=$('.down'),//上箭头
   	  upArrowImg=$('.down img'),
   		upArrowText= $('.down .text'),
   		doY=$(document).scrollTop();
   		var list=$('[data-nav]');
   		var height = 0;
   		if($('header').html()){
   			height = parseInt($('header').height());
   		}

   		var nav1=$(list[0]).offset().top-48-height,
   		nav2=$(list[1]).offset().top-48-height,
   	  nav3= $(list[2]).offset().top-48-height,
   		nav4= $(list[3]).offset().top-48-height;//分类标题
	 		this.navi.find('h2').removeClass('font-active');
	 		this.navi.find('i').hide();
   		if(doY >= nav1 && doY <nav2){
	   			this.navbar1.find('h2').addClass('font-active');
	   			this.navbar1.find('i').show();
	   			upArrow.css('opacity',0);
	 			  upArrow.css('zIndex',-2);
   		}else if(doY >= nav2 && doY < nav3){
	   			this.navbar2.find('h2').addClass('font-active');
	   			this.navbar2.find('i').show();
	   			upArrow.css('opacity',0);
	 				upArrow.css('zIndex',-2);
   		}else if(doY >= nav3 && doY < nav4){
	   			this.navbar3.find('h2').addClass('font-active');
	   			this.navbar3.find('i').show();
	   			upArrow.css('opacity',0);
	 			  upArrow.css('zIndex',-2);
   		}else if(doY >= nav4){
	   			this.navbar4.find('h2').addClass('font-active');
	   			this.navbar4.find('i').show();
	   			upArrow.css('opacity',1);
	   			upArrow.css('zIndex',200);
	   			this.upScroll(doY);
	   			$(window).scroll(function(){
						echo.init();
						// 当滚动到距底部以上200像素
						var offset = $(".sens .htb-list").offset();
						console.log(pro_load_page+':dfdfsd');
						var documentHeight = offset.top + $(".sens .htb-list").height();
						if (documentHeight - $(this).scrollTop() - $(this).height()<700){
							pro_load_flag && mainI.loadMore('/Index/Index/index');
						}
					});
   		}else{
	   			this.navi.find('h2').removeClass('nav-active');
	 			  upArrow.css('opacity',0);
	 			  upArrow.css('zIndex',-2);
	 			  upArrow.hide();
   		}
   	},
   	upScroll: function(doY){//红人推荐    上箭头
   		var upArrow=$('.down'),//上箭头
   		upArrowImg=$('.down img'),
   		upArrowText= $('.down .text');
   		var doy=doY;
   		var len =  $('.sens .htb-list li').length;
   		var li=  $('.sens .htb-list li');
 		if(doy != localStorage.getItem('sensH')){
 			if( doy >this.navbar4 && doy <= ($(li[0]).offset().top-50)){
 				upArrowText.fadeIn();
 				$('.list-num').text(1);
 				upArrowText.delay(500).fadeOut();
				upArrowImg.delay(1000).fadeIn();
 				localStorage.setItem('sensh',$(document).scrollTop());
 			}
 		}
   		 for(var i=1;i<li.length;i++){
	        	if(scrollY >= ($(li[i-1]).offset().top-100)  && scrollY < ($(li[i]).offset().top-100)){
	        		$('.down').css('display','block!important');
	        			$('.img-show').hide();
	        			$('.list-num').text($(li[i]).index()+1);
	        			$('.num-show').fadeIn();
	        			$('.num-show').delay(500).fadeOut();
	        			$('.img-show').delay(1000).fadeIn();
	        			localStorage.setItem('a',$(document).scrollTop());
	        	}
	    } 
   	},
   	addList:function(){
   		app.ajax(
			'/Index/Index/index_list',
			null,
			function(res){
				if(res.code == 200){
					app.tpl($('.today'),'tpl-today',res);
					app.tpl($('.reco'),'tpl-reco',res);
					echo.init();
				}else{
					app.prompt('洋玩易提示','系统繁忙，请稍候再试');
				}
			}
		);
   	},
   	downTime: function(){

   		    //开始时间
     	var starttimes1 = $('#st').val();
	 		starttimes1=starttimes1.replace(/-/g,"/");  
       starttimes1=new Date(starttimes1);

       starttimes1=starttimes1.getHours()*60*60*1000+starttimes1.getMinutes()*60*1000+starttimes1.getSeconds()*1000;

     var endtimes1 = $('#ed').val();
			endtimes1=endtimes1.replace(/-/g,"/");
       endtimes1=new Date(endtimes1);
       endtimes1=endtimes1.getHours()*60*60*1000+endtimes1.getMinutes()*60*1000+endtimes1.getSeconds()*1000;
        $('.ts').text('starttimes1换算'+ starttimes1+'endtimes1换算'+ endtimes1);
       var d=0,h=0,m=0,s=0;
       var NowTime = new Date();		
       var nowtimes = NowTime.getHours()*60*60*1000+NowTime.getMinutes()*60*1000+NowTime.getSeconds()*1000;
       var ts1 = starttimes1 - nowtimes;
       var te1= endtimes1 - nowtimes;   
        $('.tsi').text('ts1换算'+ starttimes1+'te1换算'+ endtimes1);
        if(nowtimes < starttimes1 ){
            if(nowtimes <starttimes1){
            	ts1=Math.abs(ts1);
             d=Math.floor(ts1/1000/60/60/24);
             h=Math.floor(ts1/1000/60/60%24);
             m=Math.floor(ts1/1000/60%60);
             s=Math.floor(ts1/1000%60);
            }
            if(h<10) h='0'+h;
            if(m<10) m='0'+m;
            if(s<10) s='0'+s;
            h=h.toString();
             m=m.toString();
              s=s.toString();
            $('.down-time .qiang-text').text('即将开抢');
            $('.time .h').text(h);
            $('.time .m').text(m);
            $('.time .s').text(s);
            $('.ms-btn').removeClass('btn-red').addClass('btn-b');
               		   
        }else if(nowtimes<endtimes1 && nowtimes>starttimes1){
            var d=0,h=0,m=0,s=0;
            if(nowtimes<endtimes1 && nowtimes>starttimes1){
            	te1=Math.abs(te1);
                d=Math.floor(te1/1000/60/60/24);
                h=Math.floor(te1/1000/60/60%24);
                m=Math.floor(te1/1000/60%60);
                s=Math.floor(te1/1000%60);
            }
            if(h<10) h='0'+h;
            if(m<10) m='0'+m;
            if(s<10) s='0'+s;
              h=h.toString();
             m=m.toString();
              s=s.toString();
            $('.down-time .qiang-text').text('正在抢购');
         	$('.time .h').text(h);
            $('.time .m').text(m);
            $('.time .s').text(s);
        }else{
        	$('.down-time .qiang-text').text('本轮抢购已结束');
        	$('.time .h').text('00');
          $('.time .m').text('00');
          $('.time .s').text('00');
          $('.time .h').hide();
          $('.time .m').hide();
          $('.time .s').hide();
          $('.time .mark').hide();
          $('.flash-num').hide();
        	$('.ms-btn').removeClass('btn-red').addClass('btn-b');
        }
         
   	},
   	loadMore: function(url){
   		var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">努力加载中~<div>';
   		$('.sens .htb-list').append(listLoadingEmpty);
   		pro_load_flag = false;
			app.ajax(
				url,
				{
					page : pro_load_page,
					limit :20
				},
				function(res){
					$('.htb-list .no-more').remove();
					if(res.code == 200){
						var list=res.h_list.list;
						if(list.length){
							pro_load_page++;
							var html='';
							for(var i in list){
								var str='';
								html+="<li class='bT' data-id='"+list[i].id+"' data-type='"+list[i].type+"'><div class='mT8'><img src='../img/index.gif' data-echo='"+list[i].pic+"' width='100% vb'/>"
									    +"</div>"+str+"<h2 class='pB6 pR10 fS14 pT7 limit-text ls1'>";
								if(list[i].guojia !=""){
									str +="<img class='nation-img' src='../img/index.gif' data-echo='"+list[i].guojiapic+"'/>";
								}
								str +=list[i].title+"</h2><p class='fS12 pB13 oF cDarkGrey ls1'>"+list[i].desc+"</p></li>";
							}
							$('.sens .htb-list').append(html);
							echo.init();
							pro_load_flag = true;
						//page++;
						}else{
							var listLoadingEmpty = '<div style="height:3rem;line-height:3rem;text-align:center;font-size:1.2rem;" class="no-more">没有更多数据了~<div>';
							$('.sens .htb-list').append(listLoadingEmpty);
						}
					}else{
						app.prompt('系统繁忙，请稍候再试');
					}
				});	
	}
}
   

  header.init('洋玩易');	
  $('header .back').empty();
  $('header .search').empty();
	footer.init();
  mainI.init();	
  


window.onload = function(){
			
      var ua = window.navigator.userAgent.toLowerCase(); 
      if(ua.match(/MicroMessenger/i) != 'micromessenger'){ 
      	$('body .index-nav').css('top','3.7rem');
      	$('.step1').css('marginTop','7.42rem');
      }else{
      	$('.step1').css('marginTop','3.7rem');
      }
      

      
      setTimeout(function(){
				var myswiper1 = new Swiper('.weidianBanner.swiper-container', {
		     		 autoplay: 3000,
		     		loop:true,
	          pagination:'.weidianBanner .swiper-pagination',
	          paginationClickable: true,
	          autoplayDisableOnInteraction : false,
	
	      });
      },3000);
      	
      $('body').on('click','.index-nav .mui-col-xs-3',function(){
		  	mainI.navJump(this);
		  });
		  

	  app.toHistory(window.location.href);

		  /*热销分类跳转**/
		  $('body').on('click','.hot-classify .mui-col-xs-6',function(){
		  	var cat_id = $(this).attr('cat_id');
		  	app.open('product.list.html','product_list',{catId:cat_id},window.location.href,document.body.scrollTop);
		  });
  		
		  $(window).scroll(function(){
				mainI.navScroll();
			});
			
//    setTimeout(function(){
//    	setInterval(function(){
//    		mainI.downTime();
//    	},1000);
//    },1000);
      /**列表内容跳转*/
     $('body').on('click','.t-list .htb-list li',function(){
	     	var id1=$(this).attr('data-id');
	     	var type=$(this).attr('data-type');
	     	if(type == 1){
	     		app.open('htb_list.html','htb_list',{id:id1},window.location.href,document.body.scrollTop);
	     	}
	     	if(type == 2){
	     		console.log(type);
	     		var ids=$(this).attr('data-goodsId');
	     		app.open('productDetail.html','productDetail',{id:ids},window.location.href,document.body.scrollTop);
	     	}
     });
     
     /*秒杀按钮直接调结算*/
     $('body').on('click','.ms-btn',function(){
     	
     });
     
     $('body').on('click','.index-nav .search',function(){
     		app.open('search.html','search',{},window.location.href,document.body.scrollTop);
     });
     /*跳转详情页*/
     $('body').on('click','.todetail',function(){
 	     		var ids=$(this).attr('data-skuId');
     		app.open('msDetail.html','productDetail',{id:ids,title:'ms'},window.location.href,document.body.scrollTop);
     });
     
     $('body').on('click','.nation-list',function(){ 	
     		app.open('nation.list.html','nationList',{},window.location.href,document.body.scrollTop);
     });
     
     $('body').on('click','.btn-red',function(){
     		app.open('accout.html','accoutL',{},window.location.href,document.body.scrollTop);
     });
     /*向上滚动箭头**/
    $('body').on('click','.down',function(){
    	  var scrollH=$($('[data-nav]')[3]).offset().top-50;
    	  $('html,body').animate({scrollTop:scrollH}, 800);
    });
    
    $('body').on('click','.hr-banner',function(){
    	 	app.open('becomeRed.html','becomeRed',{},window.location.href,document.body.scrollTop);
    });
    
    $('body').on('click','.weidianBanner .swiper-slide .btn',function(){
    	app.open('regiRed.html','regiRed',{},window.location.href,document.body.scrollTop);
    });
    
     $('body').on('click','.weidianBanner .swiper-slide.activ',function(){
    		$('#body').show();
    		$('.body-img').removeClass('dN');
    });
    
    $('body').on('click','.body-img .dele',function(){
    	  $('.body-img').addClass('dN');
    		$('#body').hide();
    		return false;
    });
    
    $('body').on('click','.ms-btn',function(){//立即秒杀
    		var skuId = $(this).attr('data-skuId'),
    		supplierId = $(this).attr('data-supplierId'),
    		data ={},
    		warehouseType = $(this).attr('data-warehouseType');
   /* 		app.ajax(
    			'/Goods/Order/order_add',
    			{
    				  supplier_id : supplierId,
        			warehouse_type :warehouseType,
       				skuIds :skuId,
        			active_id : 3//  活动  3
    			},function(res){
    					if(res.code == 200){
    						$('body').html(res.html);
    					}else{
    						layer.msg(res.msg);
    					}
    		});*/
    		data =　{
					supplier_id : supplierId,
    			warehouse_type :warehouseType,
   				skuid :skuId,
    			active_id : 3//  活动  3
    		}
    		app.ajax(
    			'/Goods/Cart/upgradehongren',
 				 data,function(res){
					if(res.code == 200){
						app.open('accout.html','accoutD',{skuid : data.skuid,data1:data},window.location.href,document.body.scrollTop);
					}else{
						layer.msg(res.msg);
					}
    		});
    });

	//关闭新用户红包
	$("body").on("click",".redPack-close",function () {
		$("#body").remove();
		$(".tC.comm div").append("<i class='cWhite' style='font-size:12px'>1</i>")
	});

	//点击领取新用户红包
	$("body").on("click",".redPack-price",function () {
		$(".weidianBanner .swiper-slide .btn").click();
	});
};